package org.jetnuke.modules.pictures.utils;

import org.apache.log4j.Logger;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import net.jforum.dao.UserDAO;
import net.jforum.entities.Theme;
import net.jforum.entities.User;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.jetnuke.common.BlockInfo;
import org.jetnuke.common.IBlockInfo;
import org.jetnuke.dao.BlogCategoryDAO;
import org.jetnuke.dao.BlogUserSettingsDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.PictureAlbumDAO;
import org.jetnuke.dao.PictureUserSettingsDAO;
import org.jetnuke.entities.BlogUserSettings;
import org.jetnuke.entities.PictureAlbum;
import org.jetnuke.entities.PictureUserSettings;

public class PictureUtils {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(PictureUtils.class);

	private PictureUtils() {
	}

	public static boolean isPictureInitialized(int uid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("isPictureInitialized(int) - start"); //$NON-NLS-1$
		}

		PictureUserSettingsDAO pusd = DataAccessDriver.getInst()
				.newPictureUserSettingsDAO();
		PictureUserSettings bus = pusd.retrieveByPk(uid);
		if (bus.getID() == 0)
			return false;

		if (logger.isDebugEnabled()) {
			logger.debug("isPictureInitialized(int) - end"); //$NON-NLS-1$
		}
		return true;
	}

	public static void manageStyleAndBlocks(VelocityContext vc, int uid)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("manageStyleAndBlocks(VelocityContext, int) - start"); //$NON-NLS-1$
		}

		PictureUserSettings pus = DataAccessDriver.getInst()
				.newPictureUserSettingsDAO().retrieveByPk(uid);
		vc.put("settings", pus);
		List preLeftBlocks = new ArrayList();
		PictureUtils.manageStyle(pus, vc, preLeftBlocks);
		UserDAO um = DataAccessDriver.getInst().newUserDAO();
		net.jforum.entities.User user = um.selectById(uid);
		vc.put("user", user);
		List albums = DataAccessDriver.getInst().newPictureAlbumDAO()
				.selectByUser(uid);
		vc.put("user_albums", albums);
		vc.put("_pagetitle", " - " + pus.getTITLE());

		preLeftBlocks.add(PictureUtils.createUserBlockInfo(vc, user));

		if (logger.isDebugEnabled()) {
			logger.debug("manageStyleAndBlocks(VelocityContext, int) - end"); //$NON-NLS-1$
		}
	}

	public static void initializePictures(int uid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initializePictures(int) - start"); //$NON-NLS-1$
		}

		PictureAlbumDAO pad = DataAccessDriver.getInst().newPictureAlbumDAO();
		List ls = pad.selectByUser(uid);
		if (ls.isEmpty()) {
			PictureAlbum pa = new PictureAlbum();
			pa.setUID(uid);
			pa.setTITLE("Untitled Album");
			pa.setDESCRIPTION("Untitled Album");
			pa.setKEYWORDS("");
			pad.addNew(pa);
		}

		PictureUserSettingsDAO pusd = DataAccessDriver.getInst()
				.newPictureUserSettingsDAO();
		PictureUserSettings pus = pusd.retrieveByPk(uid);
		if (pus.getID() == 0) {
			pus.setID(uid);
			User user = DataAccessDriver.getInst().newUserDAO().selectById(uid);
			pus.setTITLE(user.getUsername() + "'s Pictures");
			pus.setDESCRIPTION("");
			// bus.setMAXNUMBERPOST(10);
			int theme = user.getThemeId();
			if (theme == 0)
				theme = 1;
			pus.setTHEME(theme);
			pusd.insert(pus);

		}

		if (logger.isDebugEnabled()) {
			logger.debug("initializePictures(int) - end"); //$NON-NLS-1$
		}
	}

	public static void manageStyle(PictureUserSettings pus, VelocityContext vc,
			List preLeftBlocks) {
		if (logger.isDebugEnabled()) {
			logger
					.debug("manageStyle(PictureUserSettings, VelocityContext, List) - start"); //$NON-NLS-1$
		}

		String _styleFolder = "_styleFolder";
		String currentStyle = null;
		try {
			Theme tm = net.jforum.dao.DataAccessDriver.getInstance()
					.newThemeDAO().selectByID(pus.getTHEME());
			currentStyle = tm.getStyleName();
		} catch (Throwable e) {
			logger
					.warn(
							"manageStyle(PictureUserSettings, VelocityContext, List) - exception ignored", e); //$NON-NLS-1$

		}
		if (currentStyle != null)
			vc.put(_styleFolder, "style/" + currentStyle);
		vc.put("_currentStyle", currentStyle);
		vc.put("_preBlocks", preLeftBlocks);
		vc.put("_pagetitle", " - " + pus.getTITLE());

		if (logger.isDebugEnabled()) {
			logger
					.debug("manageStyle(PictureUserSettings, VelocityContext, List) - end"); //$NON-NLS-1$
		}
	}

	public static IBlockInfo createUserBlockInfo(VelocityContext vc, User us) {
		if (logger.isDebugEnabled()) {
			logger.debug("createUserBlockInfo(VelocityContext, User) - start"); //$NON-NLS-1$
		}

		StringWriter sw = new StringWriter();
		try {
			BlogUserSettingsDAO busm = DataAccessDriver.getInst()
					.newBlogUserSettingsDAO();
			BlogUserSettings bus = busm.retrieveByPk(us.getId());
			if (bus.getID() != 0) {
				vc.put("hasBlog", Boolean.TRUE);
				vc.put("blogTitle", bus.getTITLE());
			}

			Template t = Velocity
					.getTemplate("/modules/pictures/user_block.vm");
			vc.put("user", us);
			BlogCategoryDAO bcm = DataAccessDriver.getInst()
					.newBlogCategoryDAO();
			List ls = bcm.selectByUser(us.getId());
			vc.put("blogCategories", ls);
			t.merge(vc, sw);
		} catch (Exception e) {
			logger
					.warn(
							"createUserBlockInfo(VelocityContext, User) - exception ignored", e); //$NON-NLS-1$

		}
		String title = (String) vc.get("_THIS_PICTURES");
		IBlockInfo bInfo = createBlockInfo(title, sw.toString());

		if (logger.isDebugEnabled()) {
			logger.debug("createUserBlockInfo(VelocityContext, User) - end"); //$NON-NLS-1$
		}
		return bInfo;
	}

	private static IBlockInfo createBlockInfo(String title, String content) {
		if (logger.isDebugEnabled()) {
			logger.debug("createBlockInfo(String, String) - start"); //$NON-NLS-1$
		}

		BlockInfo bInfo = new BlockInfo();

		bInfo.setPath("");
		bInfo.setTitle(title);
		bInfo.setAdminOnly(false);
		bInfo.setAnonymousOnly(false);
		bInfo.setBlockClass("org.jetnuke.common.BasicBlock");
		bInfo.setContent(content);
		bInfo.setPosition("l");
		bInfo.setValid(true);
		bInfo.setActivated(true);

		if (logger.isDebugEnabled()) {
			logger.debug("createBlockInfo(String, String) - end"); //$NON-NLS-1$
		}
		return bInfo;
	}
}
